package com.wk.colorprogressbar;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewTreeObserver;


public class ColorProgressViewVertical extends View {

	private float maxCount = 100;
	private float currentCount;
	private Paint mPaint;
	private int mWidth,mHeight;

	public ColorProgressViewVertical(Context context, AttributeSet attrs, int defStyleAttr) {
		super(context, attrs, defStyleAttr);
		initView();
	}

	public ColorProgressViewVertical(Context context, AttributeSet attrs) {
		super(context, attrs);
		initView();
	}

	public ColorProgressViewVertical(Context context) {
		super(context);
		initView();
	}


	private void initView (){
		mPaint = new Paint();
		mPaint.setAntiAlias(true);

		getViewTreeObserver().addOnGlobalLayoutListener( new ViewTreeObserver.OnGlobalLayoutListener() {
			@Override
			public void onGlobalLayout() {
				round = mWidth/2;
				rectBg = new RectF(0, 0, mWidth, mHeight);
				rectBlackBg = new RectF(2, 2, mWidth-2, mHeight-2);
				getViewTreeObserver().removeOnGlobalLayoutListener(this);
			}
		} );
	}


	@Override
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
		int widthSpecMode = MeasureSpec.getMode(widthMeasureSpec);
		int widthSpecSize = MeasureSpec.getSize(widthMeasureSpec);
		int heightSpecMode = MeasureSpec.getMode(heightMeasureSpec);
		int heightSpecSize = MeasureSpec.getSize(heightMeasureSpec);

		if (widthSpecMode == MeasureSpec.EXACTLY || widthSpecMode == MeasureSpec.AT_MOST) {
			mWidth = widthSpecSize;
		} else {
			mWidth = 0;
		}
		if (heightSpecMode == MeasureSpec.AT_MOST || heightSpecMode == MeasureSpec.UNSPECIFIED) {
			mHeight = dipToPx(15);
		} else {
			mHeight = heightSpecSize;
		}
		setMeasuredDimension(mWidth, mHeight);
	}


	private int round ;
	RectF rectBg = null;
	RectF rectBlackBg  ;
	RectF rectProgressBg = new RectF(0,0 , 0  ,0 );

	@Override
	protected void onDraw(Canvas canvas) {
		super.onDraw(canvas);

		mPaint.setColor(Color.GREEN);
		canvas.drawRoundRect(rectBg, round, round, mPaint);

		mPaint.setColor(Color.BLACK);
		canvas.drawRoundRect(rectBlackBg, round, round, mPaint);

		float section = currentCount/maxCount;
		int padding = 2;
		rectProgressBg.left= padding;
		rectProgressBg.top = mHeight - ((mHeight- padding) * section) ;
		rectProgressBg.right=mWidth- padding;
		rectProgressBg.bottom= mHeight- padding;

		mPaint.setColor(getColorByValue(section) );
		canvas.drawRoundRect(rectProgressBg, round, round, mPaint);
	}

	private int  getColorByValue(float progress){
		int  color1  ;
		if(progress >=0.8  ){
			color1= Color.GREEN ;
		}
		else if(progress >=0.5){
			color1= Color.YELLOW  ;
		}

		else if(progress >0){
			color1= Color.RED ;
		}
		else{
			color1= Color.TRANSPARENT ;
		}
		return color1;
	}

	private int dipToPx(int dip) {
		float scale = getContext().getResources().getDisplayMetrics().density;
		return (int) (dip * scale + 0.5f * (dip >= 0 ? 1 : -1));
	}

	public void setMaxCount(float maxCount) {
		this.maxCount = maxCount;
	}

	public void setCurrentCount(float currentCount) {
		this.currentCount = currentCount > maxCount ? maxCount : currentCount;
		postInvalidate();
	}
}
